/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bkt.service.app.impl;

import bkt.DataFormat;
import bkt.app.facade.Facade;
import bkt.model.hr.attendance.Attendance;
import bkt.model.hr.attendance.Checkin;
import bkt.model.hr.attendance.Checkout;
import bkt.model.person.Employee;
import bkt.service.app.CheckinAndOutReportService;
import bkt.service.model.hr.attendance.CheckinService;
import bkt.service.model.hr.attendance.CheckoutService;
import bkt.service.model.person.EmployeeService;
import com.vaadin.ui.DateField;
import java.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Serge
 */
@Service("checkinAndOutReportService")
public class CheckinAndOutReportServiceImpl implements CheckinAndOutReportService {

    @Autowired
    private CheckinService checkinService;
    @Autowired
    private CheckoutService checkoutService;
    @Autowired
    private EmployeeService employeeService;
    private Facade data = new Facade();
    private DataFormat dataFormat = new DataFormat();

    @Override
    public List<Checkin> dailyCheckin(String employeeId, Date startDate, Date endDate) {
        Employee employee = employeeService.findById(employeeId);
        List<Checkin> reportList = new ArrayList<Checkin>();
        List<Checkin> checkinList = employee.getCheckins();
        for (Checkin checkinEntry : checkinList) {
            if (isWithinRange(checkinEntry.getDate(), startDate, endDate)) {
                reportList.add(checkinEntry);

            }

        }

        return checkinList;
    }

    @Override
    public List<Checkout> dailyCheckout(String employeeId, Date startDate, Date endDate) {
        Employee employee = employeeService.findById(employeeId);
        List<Checkout> reportList = new ArrayList<Checkout>();
        List<Checkout> checkoutList = employee.getCheckouts();
        for (Checkout checkoutEntry : checkoutList) {
            if (isWithinRange(checkoutEntry.getDate(), startDate, endDate)) {
                reportList.add(checkoutEntry);

            }

        }

        return checkoutList;
    }

    @Override
    public Checkin buildCheckin(Date checkinDate, Date checkinTime) {
        Checkin checkin = new Checkin();
        checkin.setDate(checkinDate);
        checkin.setTime(checkinTime);

        return checkin;

    }

    public Checkout buildCheckout(Date checkoutDate, Date checkoutTime) {
        Checkout checkout = new Checkout();
        checkout.setDate(checkoutDate);
        checkout.setTime(checkoutTime);

        return checkout;

    }

    private boolean isWithinRange(Date testDate, Date startDate, Date endDate) {
        return !(testDate.before(startDate) || testDate.after(endDate));
    }

    @Override
    public String CheckinByEmpNumber(String empNumber) {
        String strMessageFeedback = "Checkin successfully saved.";
        String strTDbegin = "<td align='center' colspan='3'";
        String strTDcolor = " style='color: green;'>";
        String strTDend = "</td>";
        boolean booFound = false;
        List< Employee> employeeList = data.getEmployeeService().findAll();
        for (Employee employee1 : employeeList) {
            String strDbEmpNumber;
            strDbEmpNumber = "null";
            if (employee1.getEmpNumber() != null) {
                strDbEmpNumber = employee1.getEmpNumber();
            }
            if (strDbEmpNumber.equals(empNumber)) {
                booFound = true;
                Checkin checkin = new Checkin();
                checkin.setDate(new Date());
                checkin.setTime(new Date());
                Employee employee = data.getEmployeeService().findById(employee1.getId());
                employee.getCheckins().add(checkin);
                data.getEmployeeService().merge(employee);
            }
        }
        if (booFound == false) {
            strMessageFeedback = "Employee not found";
            strTDcolor = " style='color: red;'>";
        }

        return strTDbegin + strTDcolor + strMessageFeedback + strTDend;
    }

    @Override
    public String CheckinByBarcode(String empBarcode) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String CheckinByFingerprint(String empFingerprint) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    //Checkout section 
    @Override
    public String CheckoutByEmpNumber(String empNumber) {
        String strMessageFeedback = "Checkout successfully saved.";
        String strTDbegin = "<td align='center' colspan='3'";
        String strTDcolor = " style='color: green;'>";
        String strTDend = "</td>";
        boolean booFound = false;
        List< Employee> employeeList = data.getEmployeeService().findAll();
        for (Employee employee1 : employeeList) {
            String strDbEmpNumber;
            strDbEmpNumber = "null";
            if (employee1.getEmpNumber() != null) {
                strDbEmpNumber = employee1.getEmpNumber();
            }
            if (strDbEmpNumber.equals(empNumber)) {
                booFound = true;
                Checkout checkout = new Checkout();
                checkout.setDate(new Date());
                checkout.setTime(new Date());
                Employee employee = data.getEmployeeService().findById(employee1.getId());
                employee.getCheckouts().add(checkout);
                data.getEmployeeService().merge(employee);
            }
        }
        if (booFound == false) {
            strMessageFeedback = "Employee not found";
            strTDcolor = " style='color: red;'>";
        }

        return strTDbegin + strTDcolor + strMessageFeedback + strTDend;
    }

    @Override
    public String CheckoutByBarcode(String empBarcode) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String CheckoutByFingerprint(String empFingerprint) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Attendance checkinCheckoutReport(String employeeNumber, Date startDate, Date endDate) {

        DateField dateFormatUtils = new DateField();


        Attendance attendance = new Attendance();

        Employee Employee = employeeService.getByPropertyName("empNumber", employeeNumber);

        System.out.println("Employee: " + Employee.getEmpName());

        List<Checkin> checkinList = Employee.getCheckins();
        List<Checkout> checkoutList = Employee.getCheckouts();

        List<Checkin> addCheckinList = new ArrayList<Checkin>();
        List<Checkout> addCheckoutList = new ArrayList<Checkout>();
        int find = 0;

        //checkin in range
        for (Checkin checkin : checkinList) {

            if (checkDateRange(startDate, endDate, checkin.getDate()) == true) {
                addCheckinList.add(checkin);
                find++;
            }
        }

        System.out.println("Checkin find: " + find);

        //checkout in range
        find = 0;
        for (Checkout checkout : checkoutList) {
            if (checkDateRange(startDate, endDate, checkout.getDate()) == true) {
                addCheckoutList.add(checkout);
                find++;
            }
        }
      
        
        //create attendance
        attendance.setCheckins(addCheckinList);
        attendance.setCheckouts(addCheckoutList);

        
        return attendance;
    }

    private boolean checkDateRange(Date startDate, Date endDate, Date compareDate) {
        boolean boo = false;
        startDate = dataFormat.addOrSubtractDayFromDate(startDate, -1);
        endDate = dataFormat.addOrSubtractDayFromDate(endDate,+1);
        if (compareDate.after(startDate) && compareDate.before(endDate)) {
            boo = true;
            System.out.println("Date found: " + compareDate.toString());
        }
        return boo;
    }
}
